package FeedMaker;

import java.net.URL;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class FeedReader extends Thread
{
	public DdanjiFeeds feeds_;
	public DdanjiEntrys entrys_;
	
	private FeedDownloader downloader_;
	
	private String save_path_ = "ddanji_radio_feeds/";	
	private int rss_check_interval_ = 1000 * 60 * 30; // 30 minute	
	
	public void run()
	{
		Hashtable<String,String> feeds = new Hashtable<String,String>();
		
		boolean first_loop = true;
		
		try {
			while( !Thread.currentThread().isInterrupted() )
			{
				feeds.clear();
				
				feeds_.get_elements( feeds );
				
				for( Enumeration< String > f = feeds.keys(); f.hasMoreElements(); )
				{					
					parse( f.nextElement() );
				}
				
				if( first_loop )
				{
					downloader_.start();
					first_loop = false;
				}
				
				Thread.sleep(rss_check_interval_);
			}
		}
		catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			feeds = null;
		}
		
		feeds = null;
	}
	
	
	public FeedReader()
	{
		feeds_ = new DdanjiFeeds(save_path_);
		entrys_ = new DdanjiEntrys(save_path_);		
		downloader_ = new FeedDownloader( this );
	}
	
	@Override
	protected void finalize() throws Throwable
	{
		downloader_.interrupt();
		super.finalize();
	}
	
	private void parse( String feed ) 
	{		
		try 
		{
			HashSet<String> inserted_entrys = new HashSet<String>();
			
			URL xml_url = new URL(feed);
			
			XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
			
			XmlPullParser parser = parserFactory.newPullParser();
			
			parser.setInput(xml_url.openStream(), "utf-8");
			
						
			DdanjiEntry entry = null;
			
			String tag = "";
			
			String feedTitle = "";
			
			boolean firstTitle = true;
			
			for(int parserEvent = parser.getEventType(); parserEvent != XmlPullParser.END_DOCUMENT; parserEvent=parser.next())
			{
				switch( parserEvent )
				{
				case XmlPullParser.START_DOCUMENT :
					break;
					
				case XmlPullParser.START_TAG :
					tag = parser.getName();
					break;
					
				case XmlPullParser.TEXT :
					if( tag.equals("title") )
					{
						if( firstTitle )
						{
							feedTitle = parser.getText();
							firstTitle = false;
						}
						else
						{
							String title = parser.getText();
							
							DdanjiEntry reulst = entrys_.get(feedTitle, title);
							
							if( title != null && reulst == null )
							{
								entry = new DdanjiEntry();
								entry.title_ = title;
								entry.number_ = entrys_.entry_count(feedTitle);
							}
							else
							{
								inserted_entrys.add(title);
								entry = null;
							}
						}
					}
					else if( tag.equals("itunes:subtitle") )
					{
						if( entry != null )
							entry.subtitle_ = parser.getText();
					}
					else if( tag.equals("pubDate") )
					{
						if( entry != null )
							entry.pubDate_ = parser.getText();
					}
					break;
					
				case XmlPullParser.END_TAG :
					if( tag.equals("enclosure") )
					{
						if( entry != null )
						{
							entry.url_ = parser.getAttributeValue(null, "url");
							entry.length_ = parser.getAttributeValue(null, "length");
							
							entrys_.add_impl(feedTitle, entry);
							
							inserted_entrys.add(entry.title_);
						}
					}
					tag = "";
					break;
				}
			}
			
			feeds_.add(feed, feedTitle);
			
			entrys_.clearOld( feedTitle, inserted_entrys );
		}
		catch (Exception e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
